<?php ob_start();
include('mysql.php');
//$json;
$session_key=$_POST['session_key'];
$code  =  $_POST['code'];
$appid = $_POST['appid'] ;
$secret = $_POST['secret'];
$name=$_POST['name'];
$url=$_POST['url'];
// echo($code);
// echo($appid);
// echo($secret);
// echo($name);
// echo($url);
$a='';
$row=mysql_query("SELECT * FROM user where id = (SELECT max(id) FROM user) ");
$back=mysql_fetch_assoc($row);
$maxx=$back['id']+1;


$URL = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
 
$apiData=file_get_contents($URL);
//echo($apiData);
$hahaha=json_decode($apiData); 
    //echo($sessionKey = $hahaha->session_key);
        $opid = $hahaha->openid;
        $session_key = $hahaha->session_key;
        $hang=mysql_query("SELECT * FROM user where opid = '$opid' ");
        $han=mysql_fetch_assoc($hang);

//echo($opid = $hahaha->openid);
$ver=mysql_query("SELECT * FROM user where opid = '$opid' ");
$hehe=mysql_fetch_assoc($ver);

if(empty($hehe)){
    $a=0;
    $sql=mysql_query( "INSERT INTO user (id,name,opid,port) VALUES ('$maxx','$name','$opid','$url')");
}
else{
    $a=1;
    $sql=mysql_query( "UPDATE user SET port = '$url',name = '$name' WHERE opid = '$opid'");
}
        $ret=array(
            'signin'=>$a,
            'openid'=>$opid,
            'session_key'=>$session_key,
            'pho_num'=>$han['pho_num'],
        );
    header('Content-Type:application/json; charset=utf-8');
    echo(json_encode($ret,JSON_UNESCAPED_UNICODE));
?>